package model;

import java.util.*;

import com.googlecode.objectify.*;
import com.googlecode.objectify.util.DAOBase;

import com.javadocmd.simplelatlng.*;
import com.javadocmd.simplelatlng.util.*;

import java.util.logging.Logger;

// Data Access Object
public class ChatDAO extends DAO{
	
	static {
		//ObjectifyService.register(EventUser.class);
	}
/*	
	public UserProfile getUserProfileById(String uid) {
		return ofy().get(UserProfile.class, uid);
	}
	
	public void updateEventUseCount(long eid, boolean isInc)
	{
		Objectify ofy = ObjectifyService.beginTransaction();
		try{
			Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
			EventInfo ei = ofy().get(eventKey);
			if(isInc)
				ei.incUseCount();
			else
				ei.desUseCount();
			ofy.put(ei);
			ofy.getTxn().commit();
		}
		finally {
			if (ofy.getTxn().isActive())
				ofy.getTxn().rollback();
		}
	}
	
	public void incEventPlusCount(long eid)
	{
		Objectify ofy = ObjectifyService.beginTransaction();
		try{
			Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
			EventInfo ei = ofy().get(eventKey);
			ei.incPlusCount();
			ofy.put(ei);
			ofy.getTxn().commit();
		}
		finally {
			if (ofy.getTxn().isActive())
				ofy.getTxn().rollback();
		}
	}

	public void incEventLikeCount(long eid)
	{
		Objectify ofy = ObjectifyService.beginTransaction();
		try{
			Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
			EventInfo ei = ofy().get(eventKey);
			ei.incLikeCount();
			ofy.put(ei);
			ofy.getTxn().commit();
		}
		finally {
			if (ofy.getTxn().isActive())
				ofy.getTxn().rollback();
		}
	}
	
	public String getUserJid(String uid)
	{
		Key<UserProfile> userKey = new Key<UserProfile>(UserProfile.class, uid);
		UserProfile up = ofy().get(userKey);
		return up.getJid();
	}

	public List<String> getEventJidList(long eid)
	{
		Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
		Query<EventUser> q = ofy().query(EventUser.class).filter("event =", eventKey);
		
		List<String> list = new ArrayList<String>();
		for (EventUser eu: q) {			
			list.add(eu.getJid());
		}				
		return list;
	}

	
	//EventUser
	public void deleteEventUser(long eid, String uid)
	{
		Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
		Key<UserProfile> userKey = new Key<UserProfile>(UserProfile.class, uid);
		Query<EventUser> q = ofy().query(EventUser.class).filter("event =", eventKey)
				.filter("user", userKey);
		Iterable<Key<EventUser>> allKeys = q.fetchKeys();
		ofy().delete(allKeys);
	}
	
	public boolean checkEventUser(long eid, String uid) {
		Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
		Key<UserProfile> userKey = new Key<UserProfile>(UserProfile.class, uid);
		Query<EventUser> q = ofy().query(EventUser.class).filter("event =", eventKey)
				.filter("user =", userKey);
		
		Key<EventUser> eukey = q.getKey();
		if(eukey == null)
			return true;
		else
			return false;			
	}
	
	public List<UserProfile> getEventUser(long eid) {
		Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
		Query<EventUser> q = ofy().query(EventUser.class).filter("event =", eventKey);
		
		List<UserProfile> list = new ArrayList<UserProfile>();
		for (EventUser eu: q) {
			UserProfile up = ofy().get(eu.getUser());
			list.add(up);
		}		
		return list;
	}
	
	//EventMsg
	public List<EventMsg> getEventMsg(long eid) {
		Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
		Query<EventMsg> q = ofy().query(EventMsg.class).filter("event =", eventKey);
		
		List<EventMsg> list = new ArrayList<EventMsg>();
		for (EventMsg eu: q) {
			UserProfile up = ofy().get(eu.getUser());
			eu.setUsername(up.getName());
			list.add(eu);
		}		
		return list;
	}
	
	//EventPlus
	public List<UserProfile> getEventPlus(long eid) {
		Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
		Query<EventPlus> q = ofy().query(EventPlus.class).filter("event =", eventKey);
		
		List<UserProfile> list = new ArrayList<UserProfile>();
		for (EventPlus ep: q) {
			UserProfile up = ofy().get(ep.getUser());
			list.add(up);
		}		
		return list;
	}
	
	//EventLike
	public List<UserProfile> getEventLike(long eid) {
		Key<EventInfo> eventKey = new Key<EventInfo>(EventInfo.class, eid);
		Query<EventLike> q = ofy().query(EventLike.class).filter("event =", eventKey);
		
		List<UserProfile> list = new ArrayList<UserProfile>();
		for (EventLike el: q) {
			UserProfile up = ofy().get(el.getUser());
			list.add(up);
		}		
		return list;
	}
*/
}
